/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * book.java
 *
 * Created on 24-11-2011, 09:58:40
 */
package view;

import appEnums.GeneralStatus;
import appEnums.Style;
import bean.EnumObject;
import bean.LoaiHang;
import bll.CategoryBLL;
import java.awt.BorderLayout;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import helper.Converter;

/**
 *
 * @author HoangNam
 */
public class GoodsMgmt extends javax.swing.JInternalFrame {

    private Converter conv;
    private CategoryBLL categBLL;
    private List<LoaiHang> categList;
    private LoaiHang categ;

    public GoodsMgmt() {
        //sun.awt.SunToolkit.checkAndSetPolicy(this, true);
        setRootPane(createRootPane());
        setLayout(new BorderLayout());
        this.closable = true;
        this.maximizable = true;
        this.resizable = true;
        this.iconable = true;
//        isIcon = false;
//        setRootPaneCheckingEnabled(true);
//        desktopIcon = new JDesktopIcon(this);
//        updateUI();
        initComponents();
        conv = new Converter();
        categBLL = new CategoryBLL();
        setEnabledButton(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabEmpDepart = new javax.swing.JTabbedPane();
        pnCategory = new javax.swing.JPanel();
        pnInfo = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        txtCategoryName = new javax.swing.JTextField();
        lblStyle = new javax.swing.JLabel();
        cbbStyle = new javax.swing.JComboBox();
        lblStatus = new javax.swing.JLabel();
        cbbStatus = new javax.swing.JComboBox();
        lblNote = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtNote = new javax.swing.JTextArea();
        btnAddCateg = new javax.swing.JButton();
        btnUpdateCateg = new javax.swing.JButton();
        btnDeleteCateg = new javax.swing.JButton();
        btnResetCateg = new javax.swing.JButton();
        txtKeyword = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCategory = new javax.swing.JTable();
        pnDepartMgmt = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableDepart = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btAddDepart = new javax.swing.JButton();
        btUpdateDepart = new javax.swing.JButton();
        btResetDepart = new javax.swing.JButton();
        btExitDepart = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        lbDepartID = new javax.swing.JLabel();
        lbDepartName = new javax.swing.JLabel();
        txtDepartName = new javax.swing.JTextField();
        txtDepartID = new javax.swing.JTextField();

        setTitle("Quản lý hàng hóa");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/image/iconFormEmp.png"))); // NOI18N
        setMinimumSize(new java.awt.Dimension(800, 600));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        tabEmpDepart.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        tabEmpDepart.setMaximumSize(new java.awt.Dimension(1280, 1024));
        tabEmpDepart.setMinimumSize(new java.awt.Dimension(800, 600));
        tabEmpDepart.setPreferredSize(new java.awt.Dimension(800, 600));

        pnCategory.setBackground(new java.awt.Color(51, 153, 255));
        pnCategory.setPreferredSize(new java.awt.Dimension(1280, 1024));

        pnInfo.setBackground(new java.awt.Color(51, 153, 255));
        pnInfo.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)), "Thông tin loại hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(255, 255, 255))); // NOI18N

        lblName.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        lblName.setForeground(new java.awt.Color(255, 255, 255));
        lblName.setText("Loại hàng");

        lblStyle.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        lblStyle.setForeground(new java.awt.Color(255, 255, 255));
        lblStyle.setText("Quy cách");

        lblStatus.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        lblStatus.setForeground(new java.awt.Color(255, 255, 255));
        lblStatus.setText("Trạng thái");

        lblNote.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        lblNote.setForeground(new java.awt.Color(255, 255, 255));
        lblNote.setText("Ghi chú");

        txtNote.setColumns(20);
        txtNote.setRows(5);
        jScrollPane3.setViewportView(txtNote);

        btnAddCateg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Add.png"))); // NOI18N
        btnAddCateg.setText("Thêm");
        btnAddCateg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddCategActionPerformed(evt);
            }
        });

        btnUpdateCateg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Edit.png"))); // NOI18N
        btnUpdateCateg.setText("Sửa");
        btnUpdateCateg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateCategActionPerformed(evt);
            }
        });

        btnDeleteCateg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Delete.png"))); // NOI18N
        btnDeleteCateg.setText("Xóa");
        btnDeleteCateg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteCategActionPerformed(evt);
            }
        });

        btnResetCateg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Reset.png"))); // NOI18N
        btnResetCateg.setText("Làm mới");
        btnResetCateg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetCategActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnInfoLayout = new javax.swing.GroupLayout(pnInfo);
        pnInfo.setLayout(pnInfoLayout);
        pnInfoLayout.setHorizontalGroup(
            pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnInfoLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblName)
                    .addComponent(lblStyle)
                    .addComponent(lblStatus)
                    .addComponent(lblNote))
                .addGap(52, 52, 52)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnInfoLayout.createSequentialGroup()
                        .addComponent(btnAddCateg)
                        .addGap(18, 18, 18)
                        .addComponent(btnUpdateCateg)
                        .addGap(21, 21, 21)
                        .addComponent(btnDeleteCateg, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnResetCateg))
                    .addComponent(txtCategoryName)
                    .addComponent(cbbStyle, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbbStatus, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane3))
                .addContainerGap(89, Short.MAX_VALUE))
        );
        pnInfoLayout.setVerticalGroup(
            pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblName)
                    .addComponent(txtCategoryName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblStyle)
                    .addComponent(cbbStyle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblStatus)
                    .addComponent(cbbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblNote)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAddCateg)
                    .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnResetCateg)
                        .addComponent(btnDeleteCateg)
                        .addComponent(btnUpdateCateg)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setText("Tìm kiếm");

        tblCategory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tên loại", "Quy cách", "Trạng thái", "Ghi chú"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblCategory.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tblCategory.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblCategory.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblCategoryMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tblCategory);

        javax.swing.GroupLayout pnCategoryLayout = new javax.swing.GroupLayout(pnCategory);
        pnCategory.setLayout(pnCategoryLayout);
        pnCategoryLayout.setHorizontalGroup(
            pnCategoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnCategoryLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnCategoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnCategoryLayout.createSequentialGroup()
                        .addComponent(txtKeyword, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        pnCategoryLayout.setVerticalGroup(
            pnCategoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnCategoryLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pnCategoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtKeyword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabEmpDepart.addTab("Quản lý loại hàng", new javax.swing.ImageIcon(getClass().getResource("/image/category.png")), pnCategory); // NOI18N

        pnDepartMgmt.setBackground(new java.awt.Color(15, 125, 178));
        pnDepartMgmt.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, new java.awt.Color(255, 255, 255), new java.awt.Color(255, 0, 0)), "DEPARTMENT MANAGEMENT", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 12), new java.awt.Color(255, 255, 255))); // NOI18N

        tableDepart.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Department name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableDepart.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableDepartMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tableDepart);

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Publisher.png"))); // NOI18N

        jPanel2.setBackground(new java.awt.Color(15, 125, 178));

        btAddDepart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Add.png"))); // NOI18N
        btAddDepart.setText("Add");
        btAddDepart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddDepartActionPerformed(evt);
            }
        });

        btUpdateDepart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Edit.png"))); // NOI18N
        btUpdateDepart.setText("Update");
        btUpdateDepart.setEnabled(false);
        btUpdateDepart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUpdateDepartActionPerformed(evt);
            }
        });

        btResetDepart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Reset.png"))); // NOI18N
        btResetDepart.setText("Reset");
        btResetDepart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btResetDepartActionPerformed(evt);
            }
        });

        btExitDepart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Exit.png"))); // NOI18N
        btExitDepart.setText("Exit");
        btExitDepart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExitDepartActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(btAddDepart, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btUpdateDepart)
                .addGap(18, 18, 18)
                .addComponent(btResetDepart)
                .addGap(18, 18, 18)
                .addComponent(btExitDepart, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btExitDepart)
                    .addComponent(btResetDepart)
                    .addComponent(btUpdateDepart)
                    .addComponent(btAddDepart))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jPanel3.setBackground(new java.awt.Color(15, 125, 178));

        lbDepartID.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        lbDepartID.setForeground(new java.awt.Color(255, 255, 255));
        lbDepartID.setText("Department ID");

        lbDepartName.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        lbDepartName.setForeground(new java.awt.Color(255, 255, 255));
        lbDepartName.setText("Department name");

        txtDepartID.setEditable(false);
        txtDepartID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDepartIDActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbDepartName)
                    .addComponent(lbDepartID))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDepartName)
                    .addComponent(txtDepartID, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE))
                .addContainerGap(44, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbDepartID)
                    .addComponent(txtDepartID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbDepartName)
                    .addComponent(txtDepartName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout pnDepartMgmtLayout = new javax.swing.GroupLayout(pnDepartMgmt);
        pnDepartMgmt.setLayout(pnDepartMgmtLayout);
        pnDepartMgmtLayout.setHorizontalGroup(
            pnDepartMgmtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDepartMgmtLayout.createSequentialGroup()
                .addGroup(pnDepartMgmtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnDepartMgmtLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnDepartMgmtLayout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 533, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnDepartMgmtLayout.createSequentialGroup()
                        .addContainerGap(83, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(100, 100, 100)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        pnDepartMgmtLayout.setVerticalGroup(
            pnDepartMgmtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDepartMgmtLayout.createSequentialGroup()
                .addGroup(pnDepartMgmtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addGroup(pnDepartMgmtLayout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabEmpDepart.addTab("Department Management", new javax.swing.ImageIcon(getClass().getResource("/image/publisher2.png")), pnDepartMgmt); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabEmpDepart, javax.swing.GroupLayout.DEFAULT_SIZE, 894, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tabEmpDepart, javax.swing.GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabEmpDepart.getAccessibleContext().setAccessibleName("Add a new book");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAddDepartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddDepartActionPerformed
        addNewDepartment();
        loadDepartment();
        loadDepartmentToTable();
        loadStatusAndStyleToCombobox();
    }//GEN-LAST:event_btAddDepartActionPerformed

    private void btUpdateDepartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUpdateDepartActionPerformed
        updateDepartmentInformation();
        loadDepartment();
        loadDepartmentToTable();
        loadStatusAndStyleToCombobox();
        loadCategory();

    }//GEN-LAST:event_btUpdateDepartActionPerformed

    private void tableDepartMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableDepartMouseClicked
        showDepartmentInformation();
        btAddDepart.setEnabled(false);
        btUpdateDepart.setEnabled(true);
    }//GEN-LAST:event_tableDepartMouseClicked

    private void btResetDepartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btResetDepartActionPerformed
        txtDepartID.setText("");
        txtDepartName.setText("");
        btAddDepart.setEnabled(true);
        btUpdateDepart.setEnabled(false);
    }//GEN-LAST:event_btResetDepartActionPerformed

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
        MainForm.goodsForm = null;
    }//GEN-LAST:event_formInternalFrameClosed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        loadCategory();
        loadStatusAndStyleToCombobox();
        loadDepartment();
        loadDepartmentToTable();
    }//GEN-LAST:event_formInternalFrameOpened

    private void btExitDepartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExitDepartActionPerformed
        this.dispose();
    }//GEN-LAST:event_btExitDepartActionPerformed

    private void txtDepartIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDepartIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDepartIDActionPerformed

    private void btnResetCategActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetCategActionPerformed
        txtCategoryName.setText("");
        cbbStatus.setSelectedIndex(0);
        cbbStyle.setSelectedIndex(0);
        txtNote.setText("");

    }//GEN-LAST:event_btnResetCategActionPerformed

    private void btnDeleteCategActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteCategActionPerformed
        deleteCategory();
    }//GEN-LAST:event_btnDeleteCategActionPerformed

    private void btnUpdateCategActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateCategActionPerformed
        updateCategInfo();
    }//GEN-LAST:event_btnUpdateCategActionPerformed

    private void btnAddCategActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddCategActionPerformed
        addNewCategory();
    }//GEN-LAST:event_btnAddCategActionPerformed

    private void tblCategoryMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCategoryMouseReleased
        showCategoryDetail();
    }//GEN-LAST:event_tblCategoryMouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddDepart;
    private javax.swing.JButton btExitDepart;
    private javax.swing.JButton btResetDepart;
    private javax.swing.JButton btUpdateDepart;
    private javax.swing.JButton btnAddCateg;
    private javax.swing.JButton btnDeleteCateg;
    private javax.swing.JButton btnResetCateg;
    private javax.swing.JButton btnUpdateCateg;
    private javax.swing.JComboBox cbbStatus;
    private javax.swing.JComboBox cbbStyle;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lbDepartID;
    private javax.swing.JLabel lbDepartName;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblNote;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblStyle;
    private javax.swing.JPanel pnCategory;
    private javax.swing.JPanel pnDepartMgmt;
    private javax.swing.JPanel pnInfo;
    private javax.swing.JTabbedPane tabEmpDepart;
    private javax.swing.JTable tableDepart;
    private javax.swing.JTable tblCategory;
    private javax.swing.JTextField txtCategoryName;
    private javax.swing.JTextField txtDepartID;
    private javax.swing.JTextField txtDepartName;
    private javax.swing.JTextField txtKeyword;
    private javax.swing.JTextArea txtNote;
    // End of variables declaration//GEN-END:variables
    private DefaultTableModel categModel;
    private DefaultTableModel modelDepartment;

    private void loadStatusAndStyleToCombobox() {
        try {
            cbbStyle.setModel(conv.convertListToComboBoxModel(Style.getEnumList()));
            cbbStatus.setModel(conv.convertListToComboBoxModel(GeneralStatus.getEnumList()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void addNewCategory() {
        if (checkCategoryInfo() == true) {
            setCatgegoryValue();
            boolean rs = categBLL.insertCategory(categ);
            if (rs == true) {
                categ = new LoaiHang("", cbbStyle.getItemCount() - 1, "", cbbStatus.getItemCount() - 1);
                setCategInfo();
                loadCategory();
                displaySucess("Thêm mới loại hàng thành công");
            } else {
                displayFailure("Thêm mới loại hàng không thành công");
            }
        }
    }

    private void loadCategory() {
        try {
            categList = categBLL.searchCategory(txtKeyword.getText());
            categModel = (DefaultTableModel) tblCategory.getModel();
            categModel.setNumRows(0);
            if (categList != null) {
                for (LoaiHang item : categList) {
                    Vector vt = new Vector();
                    vt.add(item.getTenLoai());
                    vt.add((Style.values()[item.getQuyCach()]).text());
                    vt.add((GeneralStatus.values()[item.getTrangThai()]).text());
                    vt.add(item.getGhiChu());
                    categModel.addRow(vt);
                }
            }
            tblCategory.setModel(categModel);

        } catch (Exception ex) {
            System.out.println(">>> GoodsMgmt: " + ex.getMessage());
        }
    }

    private void showCategoryDetail() {
        int index = tblCategory.getSelectedRow();
        if (index < 0) {
//            tblCategoryMouseReleased(evt);
            return;
        }
        categ = categList.get(index);
        setCategInfo();
        setEnabledButton(false);
    }

    private void updateCategInfo() {
        if (checkCategoryInfo() == true) {
            setCatgegoryValue();
            boolean rs = categBLL.updateCategory(categ);
            if (rs == true) {
                categ = new LoaiHang("", cbbStyle.getItemCount() - 1, "", cbbStatus.getItemCount() - 1);
                setCategInfo();
                loadCategory();
                setEnabledButton(true);
                displaySucess("Cập nhật thông tin loại hàng thành công");
            } else {
                displayFailure("Cập nhật thông tin loại hàng không thành công");
            }
        }
    }

    private void deleteCategory() {
        int delete = JOptionPane.showConfirmDialog(null, "Bạn có chắc chắn không?", "Xác nhận", 1, JOptionPane.YES_NO_OPTION, new javax.swing.ImageIcon("src\\image\\Question.png"));
        if (delete == 0) {
            boolean rs = categBLL.deleteCategory(categ.getLoaiHangID());
            if (rs == true) {
                categ = new LoaiHang("", cbbStyle.getItemCount() - 1, "", cbbStatus.getItemCount() - 1);
                setCategInfo();
                loadCategory();
                setEnabledButton(true);
                displaySucess("Xóa loại hàng thành công");
            } else {
                displayFailure("Xóa loại hàng không thành công");
            }
        }
    }

    private void addNewDepartment() {
//        if(txtDepartName.getText().isEmpty())
//            displayFailure("Department information is not adequate.");
//        else{
//            try {
//                control = EmployeeMgmtControl.newinit();
//                int rs = control.addNewDepartment(txtDepartName.getText().trim());
//                if(rs > 0)
//                    displaySucess("Add new department successfully.");
//                else
//                    displayFailure("Add new department unsuccessfully.");
//            } catch (Exception ex) {
//                ex.printStackTrace();
//                displayFailure("Can not load data from database server. Please try to configure database server accurately.");
//            }
//        }
    }

    private void loadDepartmentToTable() {
//        try {
//            modelDepartment = (DefaultTableModel) tableDepart.getModel();
//            modelDepartment.setNumRows(0);
//            for(int i=0; i<listDepartment.size(); i++){
//                DepartmentDTO department = new DepartmentDTO();
//                department = listDepartment.get(i);
//                Vector vtDepartment = new Vector();
//                vtDepartment.add(department.getId());
//                vtDepartment.add(department.getName());
//                modelDepartment.addRow(vtDepartment);
//            }
//        }
//        catch (Exception ex) {
//            ex.printStackTrace();
//        }
    }

    private void updateDepartmentInformation() {
//        if(txtDepartID.getText().isEmpty() | txtDepartName.getText().isEmpty())
//            displayFailure("Department information is not adequate.");
//        
//        else{
//            try {
//                control = control.newinit();
//                int rs = control.updateDepartmentInformation(Integer.valueOf(txtDepartID.getText()), txtDepartName.getText());
//                if (rs > 0) {
//                    displaySucess("Update department information successfully.");
//                } else {
//                    displayFailure("Can not update employee department because this Department ID does not exist.");
//                }
//            }
//            catch (Exception ex) {
//                ex.printStackTrace();
//                displayFailure("Can not load data from database server. Please try to configure database server accurately.");
//            }
//        }
    }

    private void showDepartmentInformation() {
        int index = tableDepart.getSelectedRow();
        txtDepartID.setText(modelDepartment.getValueAt(index, 0).toString());
        txtDepartName.setText(modelDepartment.getValueAt(index, 1).toString());
    }

    private void loadDepartment() {
//        try {
//            control = EmployeeMgmtControl.newinit();
//            listDepartment = control.loadDepartment();
//        }
//        catch (Exception ex) {
//            ex.printStackTrace();
//            displayFailure("Can not load data from database server. Please try to configure database server accurately.");
//        }
    }

    public void displayFailure(String message) {
        JOptionPane.showMessageDialog(this, message, "Lỗi", JOptionPane.ERROR_MESSAGE, new javax.swing.ImageIcon("src\\image\\error.png"));
    }

    public void displaySucess(String str) {
        JOptionPane.showMessageDialog(this, str, "Thông báo", JOptionPane.ERROR_MESSAGE, new javax.swing.ImageIcon("src\\image\\success.png"));
    }

    public void setEnabledButton(boolean flg) {
        btnAddCateg.setEnabled(flg);
        btnUpdateCateg.setEnabled(!flg);
        btnDeleteCateg.setEnabled(!flg);
    }

    private void setCategInfo() {
        txtCategoryName.setText(categ.getTenLoai());
        cbbStyle.setSelectedIndex(categ.getQuyCach());
        cbbStatus.setSelectedIndex(categ.getTrangThai());
        txtNote.setText(categ.getGhiChu());
    }

    private boolean checkCategoryInfo() {
        if (txtCategoryName.getText().trim().equals("")) {
            displayFailure("Tên loại không thể trống!");
        } else if (cbbStyle.getSelectedItem().toString().contains("---Chọn---")) {
            displayFailure("Chọn quy cách!");
        } else if (cbbStyle.getSelectedItem().toString().contains("---Chọn---")) {
            displayFailure("Chọn trạng thái!");
        } else {
            return true;
        }
        return true;
    }

    private void setCatgegoryValue() {
        int style = Integer.parseInt(((EnumObject) cbbStyle.getSelectedItem()).getValue().toString());
        int status = Integer.parseInt(((EnumObject) cbbStatus.getSelectedItem()).getValue().toString());
        if (categ == null) {
            categ = new LoaiHang();
        }
        categ.setLoaiHangID(categ.getLoaiHangID());
        categ.setTenLoai(txtCategoryName.getText().trim());
        categ.setQuyCach(style);
        categ.setTrangThai(status);
        categ.setGhiChu(txtNote.getText().trim());
    }
}
